﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum.Shared
{
    public partial class Photo : AlbumPage
    {
        private DbAccess.Photo _photo;

		public DbAccess.Photo CurrentPhoto
		{
			get
			{
				if (_photo == null)
				{
					_photo = BusinessLogics.PhotoManager.Instance.GetPhoto(UrlContext.PhotoId);
				}
				return _photo;
			}
		}

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

			this.Title = StringUtility.Format(Resources.Strings.Title_Shared_Photo, CurrentPhoto.Album.AlbumTitle, CurrentPhoto.PhotoTitle);

            imgPhoto.ImageUrl = UrlContext.BuildPhotoPipeUrl(CurrentPhoto.PhotoId, BusinessLogics.PhotoTypes.FullSize);
			lblTitle.Text = HttpUtility.HtmlEncode(CurrentPhoto.PhotoTitle);
			if (string.IsNullOrEmpty(CurrentPhoto.PhotoDescription)) { lblDescription.Text = string.Empty; }
			else { lblDescription.Text = HttpUtility.HtmlEncode(CurrentPhoto.PhotoDescription).Replace("\n", "<br />"); }
            lblCreationDate.Text = string.Format(
				CultureInfo.CurrentCulture, Resources.Strings.My_UploadDate, CurrentPhoto.PhotoCreationDate.ToLongDateString());
        }

        protected override void AppendSiteMapNodes(OpenPhotoAlbum.Controls.AlbumSiteMapProvider provider)
        {
            base.AppendSiteMapNodes(provider);

            Shared masterPage = this.Master as Shared;
            if (masterPage == null) { return; }

            DbAccess.User user = masterPage.User;
            if (user == null) { return; }

            provider.AppendSiteMapNode(Resources.Strings.SC_Home, Links.Default);
            provider.AppendSiteMapNode(
                StringUtility.Format(Resources.Strings.Shared_Title, user.UserName),
                UrlContext.BuildSharedHomeUrl(user.UserId));
            provider.AppendSiteMapNode(
				CurrentPhoto.Album.AlbumTitle,
				UrlContext.BuildSharedAlbumUrl(CurrentPhoto.AlbumId));
            provider.AppendSiteMapNode(
				CurrentPhoto.PhotoTitle,
                RawUrl);
        }
    }

    public class PhotoNotFoundException : Exception { }
}
